import 'package:flutter/material.dart';

class MyPopupPageState extends State<MyPopupPage>
    with SingleTickerProviderStateMixin {
  void _goback(String? nextRoute) {
    widget.animationController.addStatusListener((status) {
      if (status == AnimationStatus.dismissed) {
        if (nextRoute != null && nextRoute.isNotEmpty) {
          Navigator.popAndPushNamed(context, nextRoute);
        } else {
          Navigator.pop(context);
        }
      }
    });
    widget.animationController.reverse();
  }

  _gobackDirect() {
    _goback(null);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      routes: <String, WidgetBuilder>{
        "/": (context) => buildHome(context),
      },
    );
  }

  Widget buildHome(BuildContext context) {
    return Scaffold(
      key: const Key("popuppage"),
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        leading: BackButton(
          onPressed: _gobackDirect,
        ),
        title: const Text("Popup Page"),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          // Column is also a layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Invoke "debug painting" (press "p" in the console, choose the
          // "Toggle Debug Paint" action from the Flutter Inspector in Android
          // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
          // to see the wireframe for each widget.
          //
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextButton(
                onPressed: _gobackDirect, child: const Text("dismiss Page")),
          ],
        ),
      ),
    );
  }
}

@immutable
class MyPopupPage extends StatefulWidget {
  final AnimationController animationController;

  const MyPopupPage({required this.animationController, super.key});
  @override
  State<StatefulWidget> createState() {
    return MyPopupPageState();
  }
}
